import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:drm/base_bloc/base_list_bloc.dart';
import 'package:drm/http/date_result.dart';
import 'package:drm/http/http_url.dart';
import 'package:drm/http/action.dart';
import 'azd_bean.dart';
import 'azd_mx_entity.dart';
import 'package:drm/base_bloc/save_info_entity.dart';
/*
 * event;
 *
 */

class AzdNullEvent{}
 class AzdLoadDateEvent extends RefreshListEvent<AzdBean> {

   AzdLoadDateEvent(pragma,{portUrl}) :super(pragma,portUrl:portUrl);
 }

/*
  ztInfo = '已派工'
 *Type=AZD_AZMX  //安装单---安装位置,水源,水压等信息
 *
 * ztInfo = '已安装'
 *Type=GetWYM_CPBH  //安装单---用于整机获取其商品信息
  传参：XSDNumber（销售单号），CPBH（产品编号），SX(产品颜色)；
  返回：JSON格式数据。
 */
 class AzdLoadMxEvent extends ListEvent {
   AzdLoadMxEvent(params,portUrl):super(params:params,portUrl:portUrl);
 }

class AzdSaveEvent extends ListEvent {
  AzdSaveEvent(params,portUrl):super(params:params,portUrl:portUrl);
}



/*
 *
 * state
 *
 */
class AzdLoadDateState extends RefreshState<AzdBean> {
  AzdLoadDateState(total, datas,{isSuccess,message}):super(total,datas,isSuccess:isSuccess,message:message);
}


class AzdLoadMxState extends ListState {
  DateResult<List<AzdMxEntity>> reuslt ;
  AzdLoadMxState(this.reuslt);
}
class AzdSaveState extends ListState {
  DateResult<SaveInfoEntity> reuslt ;
  AzdSaveState(this.reuslt);
}
class AzdBloc extends ListBloc {

  @override
  Stream<ListState> mapEventToState(ListEvent event) async* {
    if(event is RefreshListEvent) {
      DateResult<Map> result = await event.getList();
      if (result.isSuccess) {
        yield RefreshState(result.data['total'], result.data['rows'],isSuccess: true);
      }else {
        yield RefreshState(0, [],isSuccess: false,message: '${result.message}');
      }
    }

    if (event is AzdLoadMxEvent) {
      DateResult<List<AzdMxEntity>> reuslt = await event.getListData<AzdMxEntity>();
      yield AzdLoadMxState(reuslt);
    }

    if (event is AzdSaveEvent) {
      DateResult<SaveInfoEntity> result = await event.getInfoDate<SaveInfoEntity>();
      print(result);
      yield AzdSaveState(result) ;
    }


    if(event is NullEvent) {
      yield NullState();
    }

  }
}
